package com.woniuxy.happylife.suggest.service.impl;

import com.woniuxy.happylife.entity.Type;
import com.woniuxy.happylife.suggest.dao.TypeDao;
import com.woniuxy.happylife.suggest.service.TypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author Fu
 * @Date 2022-02-16 10:28
 */
@Slf4j
@Service
public class TypeServiceImpl implements TypeService {
    @Autowired(required = false)
    RedisTemplate<String, Object> redisTemplate;
    @Autowired(required = false)
    TypeDao typeDao;

    @Override
    public List<Type> findAllType() {
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        if(ops.get("findAllType")==null){
            log.info("走数据库");
            List<Type> allType = typeDao.findAllType();
            ops.set("findAllType",allType, Duration.ofMinutes(30));
            return allType;
        }else {
            log.info("走缓存");
            Object type = ops.get("findAllType");
            List<Type>  allType = (List)type;
            return allType;
        }


    }
}
